package edu.cth.speedapp.student;

import java.util.Scanner;

import edu.cth.speedapp.Constants;
import edu.cth.speedapp.DeviceUuidFactory;
import edu.cth.speedapp.R;
import edu.cth.speedapp.R.id;
import edu.cth.speedapp.R.layout;
import edu.cth.speedapp.R.string;
import edu.cth.speedapp.asynctasks.HTTPAsyncTask;
import edu.cth.speedapp.asynctasks.ITaskCallback;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class JoinRoomActivity extends Activity implements ITaskCallback{
	private View progress;
	private View joinRoomButtom;
	private TextView text;
	private EditText editText;
	private String roomName = "";
	private String deviceUuid;
	private String baseURL;
	
	private static final String PREFS_JOIN_ROOM_NAME = "edu.cth.speedapp.student.JoinRoomActivity.roomName";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_room);
        // Android device ID
        DeviceUuidFactory deviceUuidFactory = new DeviceUuidFactory(this);
        deviceUuid = deviceUuidFactory.getDeviceUuid().toString();     
        
        baseURL = this.getString(R.string.baseURL);
        
        progress = findViewById(R.id.joinConnectionProgress);
        progress.setVisibility(View.INVISIBLE);
        
        text = (TextView) findViewById(R.id.joinConnectionStatusText);
        
        editText = (EditText) findViewById(R.id.roomName); 
        joinRoomButtom = findViewById(R.id.joinRoomButtom);
        
     // Restore room name
        SharedPreferences settings = getSharedPreferences(Constants.SHARED_PREFS_NAME, 0);
        String roomNamePrefs = settings.getString(PREFS_JOIN_ROOM_NAME, "");
        editText.setText(roomNamePrefs);
        
        
    }
    
    @Override
    protected void onPause() {
    	super.onPause();
    	
    }

    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_join_room, menu);
        return true;
      
    }*/
    public void httpTaskCallback(String results) {
    	editText.setEnabled(true);
        joinRoomButtom.setEnabled(true);
    	
    	progress.setVisibility(View.INVISIBLE);
    	//Toast.makeText(getApplicationContext(), "callback: " + results, Toast.LENGTH_SHORT).show();
    	text.setText(R.string.enter_room_name);
    	
        if(results.contains("Joined")) {  
        	Log.d("speedapp", "trying to join");
           	Intent intent = new Intent(this, StudentActivity.class);
           	Scanner scan = new Scanner(results);
           	scan.nextLine();
           	String name = scan.nextLine();
           	String mode = scan.nextLine();
    	    intent.putExtra("roomName", name);
    	    intent.putExtra("mode", mode);
    	    startActivity(intent);
    	    
    	    //Save room name for later.
    	    SharedPreferences settings = getSharedPreferences(Constants.SHARED_PREFS_NAME, 0);
    	    settings.edit().putString(PREFS_JOIN_ROOM_NAME, name).commit();

    	    
        } else if(results.contains("no room with that name")) {
        	text.setText("Could not find room");
        	
        }else{
        	
        	text.setText("Connection Error: " + results);
        }
		
        
     // If a user exists in a room then start that intent
    /*if(!result.equals("Does not exist in any room")) { 
        	Log.d("speedapp", "room: " + result);
           	Intent intent = new Intent(mainView.getContext(), InRoomActivity.class);
           	roomName = result;
    	    intent.putExtra("roomName", roomName);
    	    mainView.getContext().startActivity(intent);
        } */
        
	}
	public void onClickJoinRoom(View v){
		
        Log.d("speedapp", "Button press registered");
        
        // Android device ID
        DeviceUuidFactory deviceUuidFactory = new DeviceUuidFactory(this);
        String deviceUuid = deviceUuidFactory.getDeviceUuid().toString();        
        
        // Room input
        
        roomName = editText.getText().toString();
        
        if(roomName.length() == 0) {
        	Toast.makeText(getApplicationContext(), "You need to enter a room name", Toast.LENGTH_SHORT).show();
        }
        
        // Try to join the room
        HTTPAsyncTask joinRoom = new HTTPAsyncTask(this);
        joinRoom.execute(baseURL + "/joinRoom.php?roomID=" + roomName + "&studentID=" + deviceUuid);
       
        progress.setVisibility(View.VISIBLE);
        text.setText(R.string.connecting);
        editText.setEnabled(false);
        joinRoomButtom.setEnabled(false);
	}

	


    
}
